package com.elai.operate.service.impl;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elai.common.constant.ScheduleConstants;
import com.elai.common.utils.DateUtils;
import com.elai.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.elai.operate.mapper.OperateReferrerMapper;
import com.elai.operate.domain.OperateReferrerDO;
import com.elai.operate.service.OperateReferrerService;

/**
 * 只能推荐Service业务层处理
 *
 * @author elai
 * @date 2021-04-04
 */
@Service
public class OperateReferrerServiceImpl implements OperateReferrerService
{
    @Autowired
    private OperateReferrerMapper operateReferrerMapper;


    /**
     * 查询只能推荐
     *
     * @param id 只能推荐ID
     * @return 只能推荐
     */
    @Override
    public OperateReferrerDO selectOperateReferrerById(Long id)
    {
        return operateReferrerMapper.selectOperateReferrerById(id);
    }

    /**
     * 查询只能推荐列表
     *
     * @param operateReferrer 只能推荐
     * @return 只能推荐
     */
    @Override
    public List<OperateReferrerDO> selectOperateReferrerList(OperateReferrerDO operateReferrer)
    {
        return operateReferrerMapper.selectOperateReferrerList(operateReferrer);
    }

    /**
     * 新增只能推荐
     *
     * @param operateReferrer 只能推荐
     * @return 结果
     */
    @Override
    public int insertOperateReferrer(OperateReferrerDO operateReferrer)
    {
        operateReferrer.setCreateTime(DateUtils.getNowDate());
        JSONObject jsonData=extractedJson(operateReferrer);
        operateReferrer.setStatus("01");
        int result= operateReferrerMapper.insertOperateReferrer(operateReferrer);
        return result;
    }

    private JSONObject extractedJson(OperateReferrerDO operateReferrer) {
        String touchType= operateReferrer.getTouchType();
        JSONObject jsonData=new JSONObject();
        //第一步
        JSONObject step1=new JSONObject();
        step1.put("name", operateReferrer.getName());
        step1.put("title", operateReferrer.getTitle());
        step1.put("channel", operateReferrer.getChannel());
        step1.put("touchType", operateReferrer.getTouchType());
        JSONObject touchDate=new JSONObject();
        if("1".equals(touchType)){
            touchDate.put("touchDate", operateReferrer.getTouchDate());
            touchDate.put("touchTime", operateReferrer.getTouchTime());
            String []dateArray=operateReferrer.getTouchDate().split("-");
            String []timeArray=operateReferrer.getTouchTime().split(":");
            String cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+dateArray[2]+" "+dateArray[1]+" ? "+dateArray[0];
            operateReferrer.setCronExpression(cronExpression);
            touchDate.put("cronExpression",cronExpression);
        }else if("2".equals(touchType)){
            touchDate.put("touchDateType", operateReferrer.getTouchDateType());
            touchDate.put("touchDateValue", operateReferrer.getTouchDateValue());
            touchDate.put("touchDate", operateReferrer.getTouchDate());
            touchDate.put("touchTime", operateReferrer.getTouchTime());
            String beginStr=DateUtils.dateTime(operateReferrer.getBeginDate())+" 00:00:00";
            operateReferrer.setBeginDate(DateUtils.parseDate(beginStr));
            touchDate.put("beginDate",beginStr);

            String endStr=DateUtils.dateTime(operateReferrer.getEndDate())+" 23:59:59";
            operateReferrer.setEndDate(DateUtils.parseDate(endStr));
            touchDate.put("endDate",endStr);
            touchDate.put("partLimit", operateReferrer.getPartLimit());
            if("2".equals(operateReferrer.getPartLimit())){
                touchDate.put("partLimitDays", operateReferrer.getPartLimitDays());
                touchDate.put("partLimitTimes", operateReferrer.getPartLimitTimes());
            }
            Date beginDate= operateReferrer.getBeginDate();
            LocalDate begin=LocalDate.parse(DateUtils.parseDateToStr("yyyy-MM-dd",beginDate));
            Date endDate1= operateReferrer.getEndDate();
            LocalDate endDate= LocalDate.parse(DateUtils.parseDateToStr("yyyy-MM-dd",endDate1));
            String []timeArray=operateReferrer.getTouchTime().split(":");
            String cronExpression="";
            if("1".equals(operateReferrer.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+begin.getDayOfMonth()+"-"+ endDate.getDayOfMonth()+" "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" ? "+"*";
            }
            else if("2".equals(operateReferrer.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" ? "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" "+operateReferrer.getTouchDateValue();
            }
            else if("3".equals(operateReferrer.getTouchDateType())){
                cronExpression="0 "+timeArray[1]+" "+timeArray[0]+" "+operateReferrer.getTouchDateValue().replace("号","")+" "+begin.getMonthValue()+"-"+ endDate.getMonthValue()+" ? ";
            }
            operateReferrer.setCronExpression(cronExpression);
            touchDate.put("cronExpression",cronExpression);
        }
        step1.put("touchDateRule",touchDate);
        operateReferrer.setStep1Json(step1.toString());
        jsonData.put("step1",step1);
        //第二步
        JSONObject step2=new JSONObject();
        step2.put("personType", operateReferrer.getPersonType());
        String personTags= operateReferrer.getPersonTags();
        JSONArray array=new JSONArray();
        if(StringUtils.isNotEmpty(personTags)){
            String [] tags=personTags.split(",");
            for(String id:tags){
                JSONObject person=new JSONObject();
                person.put("tagName","tagName");
                person.put("tagValue",id);
                array.add(person);
            }
        }
        step2.put("personTags",array);
        operateReferrer.setStep2Json(step2.toString());
        jsonData.put("step2",step2);

        //第三步
        JSONObject step3=new JSONObject();
        step3.put("translateTime", operateReferrer.getTranslateTime());
        step3.put("translateType", operateReferrer.getTranslateType());

        JSONArray ruleAry=(JSONArray) JSONObject.parse(operateReferrer.getRuleContent());
        JSONArray ruleArray=new JSONArray();
        for(int i=0;i<ruleAry.size();i++){
            JSONObject obj=(JSONObject)ruleAry.get(i);
            JSONObject rule=new JSONObject();
            rule.put("eventType",obj.getString("eventType"));
            rule.put("eventKey",obj.getString("eventKey"));
            rule.put("eventValue",obj.getString("eventValue"));
            ruleArray.add(rule);
        }
        step3.put("roleContent",ruleArray );
        operateReferrer.setStep3Json(step3.toString());
        jsonData.put("step3",step3);
        return jsonData;
    }

    /**
     * 修改只能推荐
     *
     * @param operateReferrer 只能推荐
     * @return 结果
     */
    @Override
    public int updateOperateReferrer(OperateReferrerDO operateReferrer)
    {
        return operateReferrerMapper.updateOperateReferrer(operateReferrer);
    }

    /**
     * 批量删除只能推荐
     *
     * @param ids 需要删除的只能推荐ID
     * @return 结果
     */
    @Override
    public int deleteOperateReferrerByIds(Long[] ids)
    {
        return operateReferrerMapper.deleteOperateReferrerByIds(ids);
    }

    /**
     * 删除只能推荐信息
     *
     * @param id 只能推荐ID
     * @return 结果
     */
    @Override
    public int deleteOperateReferrerById(Long id)
    {
        return operateReferrerMapper.deleteOperateReferrerById(id);
    }
}
